package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasAnalyseStationRankingEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.entity.GasStaytimeResultEntity;
import cn.com.gas.service.GasAnalyseStationRankingService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.mapper.SysDeptMapper;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 加油笔数统计
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-04-15 20:42:03
 */
@Controller
@RequestMapping("/gas/collect/refuel")
public class GasCollectRefuelController {
	
	@Autowired
	private GasStationinfoService gasStationinfoService;
	@Autowired
	private GasAnalyseStationRankingService analyseStationRanking;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	
	@Autowired
	private HttpSessionService httpSessionService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	// @RequiresPermissions("gasCollectSell:list")
	@ResponseBody
	public DataResult findListByPage(@RequestBody GasStaytimeResultEntity vo) throws ParseException {
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		Page page = new Page(vo.getPage(), vo.getLimit());
		LambdaQueryWrapper<GasStationinfoEntity> queryWrapper = Wrappers.lambdaQuery();
		
		if(TsBqUtils.isNotNull(vo.getTaxorgcode())){
			queryWrapper.eq(GasStationinfoEntity::getType, "group");
			queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, vo.getTaxorgcode());
			
		}else{
			if(Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
				// 县区局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);
				
			}else if(Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))){
				// 市局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				queryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0,5));
			}else{
				// 省局
				queryWrapper.eq(GasStationinfoEntity::getType, "group");
				queryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
			}
			
			
		}
		
		if (TsBqUtils.isNotNull(vo.getStationName())) {
			queryWrapper.like(GasStationinfoEntity::getName, vo.getStationName());
		}

		IPage<GasStationinfoEntity> iPage = gasStationinfoService.page(page, queryWrapper);
		List<GasStationinfoEntity> records = iPage.getRecords();
		
		for (GasStationinfoEntity gasStationinfoEntity : records) {
			// 处理加油站 名称
			if (gasStationinfoEntity.getType().equals("group") || gasStationinfoEntity.getType().equals("domain")) {
				gasStationinfoEntity.setName(TsBqUtils.handleEndBeforeParentheses(gasStationinfoEntity.getName()));
			}
			
			String id = gasStationinfoEntity.getId();

			LambdaQueryWrapper<GasAnalyseStationRankingEntity> queryWrapper1 = Wrappers.lambdaQuery();
			
			queryWrapper1.eq(GasAnalyseStationRankingEntity::getYearMonths, vo.getYearMonths());
			
			queryWrapper1.eq(GasAnalyseStationRankingEntity::getStationId, id);

			List<GasAnalyseStationRankingEntity> list = analyseStationRanking.list(queryWrapper1);
			
			if(TsBqUtils.isNotNull(list)){
				
				GasAnalyseStationRankingEntity entity = list.get(0);
				String amountDiesel = entity.getAmountDiesel();
				String amountGasoline = entity.getAmountGasoline();
				BigDecimal _amountDiesel = new BigDecimal(amountDiesel);
		        BigDecimal _amountGasoline = new BigDecimal(amountGasoline);
		 
		        // 使用add方法进行加法运算
		        BigDecimal amount = _amountDiesel.add(_amountGasoline);
		 
				gasStationinfoEntity.setAmount(String.valueOf(amount));
				
			}
			
			
			
			
			String taxorgcode = gasStationinfoEntity.getTaxorgcode();
			
			if(TsBqUtils.isNotNull(taxorgcode)){
				SysDept sysDept = sysDeptMapper.selectById(taxorgcode);
				if(TsBqUtils.isNotNull(sysDept)){
					String name = sysDept.getName();
					gasStationinfoEntity.setTaxorgName(name);
				}
			}
			

		}

		return DataResult.success(iPage);

	}

}
